package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.Topic;
import com.sxkf.template.service.TopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.util.CheckEmptyUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-03 20:05:24
 */
@Api(tags = "话题模块")
@RestController
@RequestMapping("/topic")
public class TopicController {
    @Resource
    private TopicService topicService;

    @ApiOperation("查询话题")
    @GetMapping("/list")
    public R<List<Topic>> list(@RequestParam(required = false) String type){
        return R.success(topicService.list(new LambdaQueryWrapper<Topic>()
                .eq(CheckEmptyUtil.isNotEmpty(type),Topic::getType,type)));
    }

    @ApiOperation("分页查询话题")
    @GetMapping("/page")
    public R<IPage<Topic>> page(@RequestParam Integer pageNo,
                                @RequestParam Integer pageSize,
                                @RequestParam(required = false) String type){

        Page<Topic> page = topicService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<Topic>()
                .eq(CheckEmptyUtil.isNotEmpty(type), Topic::getType, type));
        return R.success(page);
//        return R.success(topicService.list(new LambdaQueryWrapper<Topic>()
//                .eq(CheckEmptyUtil.isNotEmpty(type),Topic::getType,type)));
    }

    @ApiOperation("新增话题")
    @PostMapping("/add")
    public R<?> add(@RequestBody Topic topic){
        topic.setIsDelete("0");
        topicService.save(topic);
        return R.success();
    }

    @ApiOperation("删除话题")
    @PostMapping("/delete")
    public R<?> delete(@RequestParam String id){
        topicService.removeById(id);
        return R.success();
    }

    @ApiOperation("修改话题")
    @PostMapping("/update")
    public R<?> update(@RequestBody Topic topic){
        topicService.updateById(topic);
        return R.success();
    }
}
